////////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2010 galaxy.org
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

package org.galaxy.richflex.utils
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.system.ApplicationDomain;
	
	import mx.controls.Alert;
	import mx.events.ModuleEvent;
	import mx.managers.CursorManager;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;
	
	/**
	 * Module helper class. There are some useful functions used for modules
	 * operations, such as load or error handler.
	 * 
	 * @author Cheng Liang
	 * 
	 * @version 0.0.1 create 2010-9-21;
	 * @version 0.0.1a renamed from <code>ModuleUtils</code> to <code>ModuleHelper</code> 2010-9-27;
	 */
	public class ModuleHelper extends EventDispatcher
	{
		// --------------------------------------------------------------------------
		//
		// Properties
		//
		// --------------------------------------------------------------------------
		
		// ------------------------------
		// module which is loading
		// ------------------------------
		
		private var moduleInfo : IModuleInfo;
		
		// --------------------------------------------------------------------------
		//
		// Other methods
		//
		// --------------------------------------------------------------------------
		
		public function load(url : String,
							 loadSuccess : Function,
							 loading : Function = null,
							 loadFail : Function = null) : void
		{
			moduleInfo = ModuleManager.getModule(url);
			moduleInfo.addEventListener(ModuleEvent.READY, loadSuccess);
			if(loadFail == null) {
				moduleInfo.addEventListener(ModuleEvent.ERROR, loadModuleFail);
			}
			if(loading == null) {
				moduleInfo.addEventListener(ModuleEvent.PROGRESS, loadingModule);
			}
			CursorManager.getInstance().setBusyCursor();
			moduleInfo.load(ApplicationDomain.currentDomain);
		}
		
		// --------------------------------------------------------------------------
		//
		// Events handlers
		//
		// --------------------------------------------------------------------------
		
		/**
		 * Callbacks when module is loading.
		 * 
		 * @param event module event
		 * 
		 * @see mx.events.ModuleEvent#PROGRESS
		 */
		public function loadingModule(event : ModuleEvent) : void
		{
			trace("Total: " + event.bytesTotal + "; loaded: " + event.bytesLoaded);
		}
		
		/**
		 * Callbacks when module loaded failed.
		 * 
		 * @param event module event
		 * 
		 * @see mx.events.ModuleEvent#ERROR
		 */
		public function loadModuleFail(event : ModuleEvent) : void
		{
			Alert.show(event.module.url + " loaded failed.");
		}
		
		// --------------------------------------------------------------------------
		//
		// Constructor
		//
		// --------------------------------------------------------------------------
		
		public function ModuleHelper()
		{
			
		}
	}
}